package edu.ccut.saturn.studio.logic.model.drop;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.internal.core.JarEntryFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnActionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnInformationNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnStartNode;
import edu.ccut.saturn.studio.logic.model.xml.loader.InformationNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.StartNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.action.SaturnStartNodeInActionLoader;

public class ActionDropHandler extends AbstarctDropHandler {

	private static final String ACTION_DROP_HANDLER_TWO_DOT = "::";

	public static final String FILE_POSTFIX = ".action";

	private static final InformationNodeLoader informationNodeLoader = new InformationNodeLoader();

	private static final StartNodeLoader startNodeLoader = new StartNodeLoader();

	public Shape getShape(String filePath, IProject project) {
		String symbolicName = super.getBundleName(project);
		Element root = null;
		try {
			root = SaturnW3CUtil.getDocument(filePath).getDocumentElement();
		} catch (XMLException e) {
			e.printStackTrace();
		}

		return getShape(root, symbolicName);
	}
	
	public Shape getShape(JarEntryFile jarEntryFile) {
		InputStream in = null;
		
		try {
			DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
	
			in = (InputStream) jarEntryFile.getContents();
		
			Document doc = dombuilder.parse(in);
			String bundleName = super.getBundleNameFromJar(jarEntryFile);
			Element root = doc.getDocumentElement();
			
			return this.getShape(root, bundleName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return null;
	}

	private Shape getShape(Element root, String symbolicName) {
		try {
			SaturnActionNode actionNode = new SaturnActionNode();

			List<Shape> informations = informationNodeLoader.loadNodes(root);

			if (informations != null && informations.size() > 0) {
				SaturnInformationNode informationNode = (SaturnInformationNode) informations
						.get(0);

				String key = informationNode.getKey();
				String description = informationNode.getDescription();
				String name = informationNode.getName();

				actionNode.setDescription(description);
				actionNode.setKey(symbolicName.trim()
						+ ACTION_DROP_HANDLER_TWO_DOT +key);
				actionNode.setName(name);
			}

			List<Shape> starts = startNodeLoader.loadNodes(root);

			if (starts != null && starts.size() > 0) {
				SaturnStartNode startNode = (SaturnStartNode) starts.get(0);
				actionNode.setParameters(startNode.getParameters());
			}

			return actionNode;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
